# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :appraise.py
# @Time      :2022/8/1 11:02
# @Author    :吕华龙
from flask import Blueprint, jsonify
from flask_restful import Api, Resource, reqparse, marshal
from models.model import *
from models.fields import *
from flask_sqlalchemy import Pagination

appraise_bp = Blueprint("appraise_bp", __name__, url_prefix="/appraise")

api = Api(appraise_bp)


# 评论视图
class AppraiseView(Resource):
    # 获取所有的评论分页展示
    def get(self):
        # 获取课程id
        resp = reqparse.RequestParser()
        resp.add_argument("course_id", type=int)
        resp.add_argument("page", type=int)
        args = resp.parse_args()
        # 查询课程下的评论分页展示
        appraise: Pagination = Appraise.query.filter(Appraise.course_id == args['course_id']).paginate(args['page'], 3,
                                                                                                       error_out=False)
        print("-------------", appraise, appraise.items)
        # 表中所有数据的条数
        total = appraise.total
        # 当前页所有的数据
        items = appraise.items
        # 当前是第几页
        current_page = appraise.page
        print("---------", items)
        # 首先序列化数据 在构建数据结构
        appraise_marshal = marshal(items, appraise_fields)
        list_1 = {
            "total": total,
            "current_page": current_page,
            "list": appraise_marshal
        }
        return jsonify({'code': 200, 'msg': '获取评论成功', 'data': list_1})


# 创建路由
api.add_resource(AppraiseView, "/appraise")
